/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.opgea.qms.dao.impl;

import com.opgea.qms.dao.DestinationDAO;
import com.opgea.qms.domain.entities.Destination;
import com.opgea.qms.domain.model.Person;
import java.util.List;




import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Ramesh
 */
@Repository
@Transactional
public class DestinationDAOImpl implements DestinationDAO{

    
    @Autowired
    private SessionFactory sessionFactory;
    
    
    @Override
    public Destination create(Destination destination) {
        sessionFactory.openSession().save(destination);
        return destination;
    }

    @Override
    public Destination update(Destination destination) {
        Session session = sessionFactory.openSession();
        Transaction tx = session.getTransaction();
        tx.begin();
        Destination dest = (Destination) session.get(Destination.class, destination.getId());
        dest.setName(destination.getName());
        tx.commit();
        return dest;
    }

    @Override
    public Destination find(Long id) {
        Session session = sessionFactory.openSession();
        Query query = session.createQuery("from Destination d WHERE d.id = "+id);
        /*
        Query query = session.getNamedQuery("Destination.findById")
                .setParameter("id", id);
         * 
         */
        Destination destination = (Destination) query.uniqueResult();
        session.close();
        return destination;
    }

    @Override
    public List<Destination> findAll() {
        List<Destination> destinations = sessionFactory.openSession()
                .getNamedQuery("Destination.findAll").list();
        return destinations;
    }

    @Override
    public List<Destination> findAllByCompany(Long companyId) {
        List<Destination> destinations = sessionFactory.openSession()
                .createQuery("from Destination d where d.company.id = :companyId")
                .setParameter("companyId", companyId)
                .list();
        return destinations;
    }


}
